package com.hxwb.sdk.controller;

import com.hxwb.sdk.manage.ChainManage;
import com.hxwb.sdk.param.*;
import com.hxwb.sdk.util.SHAUtil;
import org.chainmaker.sdk.ResponseInfo;
import org.springframework.web.bind.annotation.*;
import org.web3j.utils.Numeric;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/chainmaker")
public class ChainMakerController {


    @GetMapping(value = "/balanceOf")
    public BalanceResponse balanceOf(@RequestParam(value = "address") String address, @RequestParam("symbol") String symbol) {
        ChainManage chainMaker = new ChainManage(symbol);
        ResponseInfo responseInfo = chainMaker.balanceOf(address);
        BalanceResponse response = new BalanceResponse();
        BigInteger bigInteger = Numeric.toBigInt(responseInfo.getSyncResultTxInfo().getTransaction().getResult().getContractResult().getResult().toByteArray());
        BigDecimal amount = new BigDecimal(bigInteger).divide(new BigDecimal(1000000));
        response.setAmount(amount);
        response.setTxId(responseInfo.getTxId());
        return response;

    }

    @PostMapping(value = "/transfer")
    public TransferResponse transfer(@RequestBody TransferParam transferParam) {
        TransferResponse transferResponse = new TransferResponse();
        transferResponse.setAmount(transferParam.getAmount());
        transferResponse.setFromAddress(transferParam.getFromAddress());
        transferResponse.setToAddress(transferParam.getToAddress());
        String encodeStr = SHAUtil.shaEncode(transferParam.getFromAddress());
        if (!transferParam.getPrivateKey().equals(encodeStr)) {
            transferResponse.setResult(Boolean.FALSE);
            transferResponse.setMessage("error privateKey!");
            return transferResponse;
        }
        ChainManage chainMaker = new ChainManage(transferParam.getSymbol());
        long amount = transferParam.getAmount().multiply(new BigDecimal(1000000)).longValue();
        ResponseInfo responseInfo = chainMaker.transfer(transferParam.getFromAddress(), transferParam.getToAddress(), amount);

        transferResponse.setTxId(responseInfo.getTxId());
        //判定请求成功
        if (responseInfo.getTxResponse().getMessage().equals("OK") && responseInfo.getSyncResultTxInfo().getTransaction().getResult().getCode().toString().equals("SUCCESS")) {
            transferResponse.setBlockHeight(responseInfo.getSyncResultTxInfo().getBlockHeight());
            transferResponse.setResult(Boolean.TRUE);
            transferResponse.setGasUsed(responseInfo.getSyncResultTxInfo().getTransaction().getResult().getContractResult().getGasUsed());
            transferResponse.setMessage("OK");
        } else {
            transferResponse.setResult(Boolean.FALSE);
            transferResponse.setMessage(responseInfo.getSyncResultTxInfo().getTransaction().getResult().getContractResult().getMessage());
        }
        return transferResponse;
    }

    @PostMapping(value = "/addHashCode")
    public AddHashCodeResponse addHashCode(@RequestBody AddHashCodeParam addHashCodeParam) {
        AddHashCodeResponse addHashCodeResponse = new AddHashCodeResponse();
        String encodeStr = SHAUtil.shaEncode(addHashCodeParam.getAddress());
        if (!addHashCodeParam.getPrivateKey().equals(encodeStr)) {
            addHashCodeResponse.setResult(Boolean.FALSE);
            addHashCodeResponse.setMessage("error privateKey!");
            return addHashCodeResponse;
        }
        ChainManage chainMaker = new ChainManage(addHashCodeParam.getSymbol());
        ResponseInfo responseInfo = chainMaker.addHashCode(addHashCodeParam.getAddress(), addHashCodeParam.getHashCode());

        addHashCodeResponse.setTxId(responseInfo.getTxId());
        //判定请求成功
        if (responseInfo.getTxResponse().getMessage().equals("OK")) {
            addHashCodeResponse.setResult(Boolean.TRUE);
            addHashCodeResponse.setMessage("OK");
        } else {
            addHashCodeResponse.setResult(Boolean.FALSE);
            addHashCodeResponse.setMessage(responseInfo.getSyncResultTxInfo().getTransaction().getResult().getContractResult().getMessage());
        }
        return addHashCodeResponse;
    }

    @GetMapping(value = "/getHashCodeList")
    public GetHashCodeResponse getHashCodeList(@RequestParam(value = "address") String address, @RequestParam("symbol") String symbol) throws Exception {
        ChainManage chainMaker = new ChainManage(symbol);
        ResponseInfo responseInfo = chainMaker.getHashCode(address);
        GetHashCodeResponse response = new GetHashCodeResponse();
        response.setAddress(address);
        List<String> hashCodeList = new ArrayList<>();
        try {
             String[] result = responseInfo.getSyncResultTxInfo().getTransaction().getResult().getContractResult().getResult().toStringUtf8().split("@");
            if (result != null && result.length != 0) {
                for (String x : result) {
                    x.trim();
                    if(x.startsWith("Qm")){
                        x = x.substring(0,46);
                        hashCodeList.add(x);
                    }
                }
            }
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        }
        response.setHashCodeList(hashCodeList);
        return response;

    }
}
